home *** CD-ROM | disk | FTP | other *** search
/ CD BIT 75 / CD BIT 75.iso / Software / mysql-4.0.22-win / data1.cab / Development / scripts / mysqld_safe < prev    next >
Encoding:
Text File  |  2004-10-28  |  10.7 KB  |  353 lines

  1. #!/bin/sh
  2. # Copyright Abandoned 1996 TCX DataKonsult AB & Monty Program KB & Detron HB
  3. # This file is public domain and comes with NO WARRANTY of any kind
  4. #
  5. # scripts to start the MySQL daemon and restart it if it dies unexpectedly
  6. #
  7. # This should be executed in the MySQL base directory if you are using a
  8. # binary installation that has other paths than you are using.
  9. #
  10. # mysql.server works by first doing a cd to the base directory and from there
  11. # executing mysqld_safe
  12.  
  13. KILL_MYSQLD=1;
  14.  
  15. trap '' 1 2 3 15            # we shouldn't let anyone kill us
  16.  
  17. umask 007
  18.  
  19. defaults=
  20. case "$1" in
  21.     --no-defaults|--defaults-file=*|--defaults-extra-file=*)
  22.       defaults="$1"; shift
  23.       ;;
  24. esac
  25.  
  26. parse_arguments() {
  27.   # We only need to pass arguments through to the server if we don't
  28.   # handle them here.  So, we collect unrecognized options (passed on
  29.   # the command line) into the args variable.
  30.   pick_args=
  31.   if test "$1" = PICK-ARGS-FROM-ARGV
  32.   then
  33.     pick_args=1
  34.     shift
  35.   fi
  36.  
  37.   for arg do
  38.     case "$arg" in
  39.       --skip-kill-mysqld*)
  40.         KILL_MYSQLD=0;
  41.         ;;
  42.       # these get passed explicitly to mysqld
  43.       --basedir=*) MY_BASEDIR_VERSION=`echo "$arg" | sed -e "s;--basedir=;;"` ;;
  44.       --datadir=*) DATADIR=`echo "$arg" | sed -e "s;--datadir=;;"` ;;
  45.       --pid-file=*) pid_file=`echo "$arg" | sed -e "s;--pid-file=;;"` ;;
  46.       --user=*) user=`echo "$arg" | sed -e "s;--[^=]*=;;"` ; SET_USER=1 ;;
  47.  
  48.       # these two might have been set in a [mysqld_safe] section of my.cnf
  49.       # they are added to mysqld command line to override settings from my.cnf
  50.       --socket=*)  mysql_unix_port=`echo "$arg" | sed -e "s;--socket=;;"` ;;
  51.       --port=*)    mysql_tcp_port=`echo "$arg" | sed -e "s;--port=;;"` ;;
  52.  
  53.       # mysqld_safe-specific options - must be set in my.cnf ([mysqld_safe])!
  54.       --ledir=*)   ledir=`echo "$arg" | sed -e "s;--ledir=;;"` ;;
  55.       # err-log should be removed in 5.0
  56.       --err-log=*) err_log=`echo "$arg" | sed -e "s;--err-log=;;"` ;;
  57.       --log-error=*) err_log=`echo "$arg" | sed -e "s;--log-error=;;"` ;;
  58.       # QQ The --open-files should be removed in 5.0
  59.       --open-files=*) open_files=`echo "$arg" | sed -e "s;--open-files=;;"` ;;
  60.       --open-files-limit=*) open_files=`echo "$arg" | sed -e "s;--open-files-limit=;;"` ;;
  61.       --core-file-size=*) core_file_size=`echo "$arg" | sed -e "s;--core-file-size=;;"` ;;
  62.       --timezone=*) TZ=`echo "$arg" | sed -e "s;--timezone=;;"` ; export TZ; ;;
  63.       --mysqld=*)   MYSQLD=`echo "$arg" | sed -e "s;--mysqld=;;"` ;;
  64.       --mysqld-version=*)
  65.     tmp=`echo "$arg" | sed -e "s;--mysqld-version=;;"`
  66.     if test -n "$tmp"
  67.     then
  68.       MYSQLD="mysqld-$tmp"
  69.     else
  70.       MYSQLD="mysqld"
  71.     fi
  72.     ;;
  73.       --nice=*) niceness=`echo "$arg" | sed -e "s;--nice=;;"` ;;
  74.       *)
  75.         if test -n "$pick_args"
  76.         then
  77.           # This sed command makes sure that any special chars are quoted,
  78.           # so the arg gets passed exactly to the server.
  79.           args="$args "`echo "$arg" | sed -e 's,\([^a-zA-Z0-9_.-]\),\\\\\1,g'`
  80.         fi
  81.         ;;
  82.     esac
  83.   done
  84. }
  85.  
  86.  
  87. MY_PWD=`pwd`
  88. # Check if we are starting this relative (for the binary release)
  89. if test -d $MY_PWD/data/mysql -a -f ./share/mysql/english/errmsg.sys -a \
  90.  -x ./bin/mysqld
  91. then
  92.   MY_BASEDIR_VERSION=$MY_PWD        # Where bin, share and data are
  93.   ledir=$MY_BASEDIR_VERSION/bin        # Where mysqld is
  94.   DATADIR=$MY_BASEDIR_VERSION/data
  95.   if test -z "$defaults"
  96.   then
  97.     defaults="--defaults-extra-file=$MY_BASEDIR_VERSION/data/my.cnf"
  98.   fi
  99. # Check if this is a 'moved install directory'
  100. elif test -f ./var/mysql/db.frm -a -f ./share/mysql/english/errmsg.sys -a \
  101.  -x ./libexec/mysqld
  102. then
  103.   MY_BASEDIR_VERSION=$MY_PWD        # Where libexec, share and var are
  104.   ledir=$MY_BASEDIR_VERSION/libexec    # Where mysqld is
  105.   DATADIR=$MY_BASEDIR_VERSION/var
  106. else
  107.   MY_BASEDIR_VERSION=/usr/local/mysql
  108.   DATADIR=/usr/local/mysql/var
  109.   ledir=/usr/local/mysql/libexec
  110. fi
  111.  
  112. user=mysql
  113. niceness=0
  114.  
  115. # Use the mysqld-max binary by default if the user doesn't specify a binary
  116. if test -x $ledir/mysqld-max
  117. then
  118.   MYSQLD=mysqld-max
  119. else
  120.   MYSQLD=mysqld
  121. fi
  122.  
  123. # these rely on $DATADIR by default, so we'll set them later on
  124. pid_file=
  125. err_log=
  126.  
  127. # Get first arguments from the my.cnf file, groups [mysqld] and [mysqld_safe]
  128. # and then merge with the command line arguments
  129. if test -x ./bin/my_print_defaults
  130. then
  131.   print_defaults="./bin/my_print_defaults"
  132. elif test -x /usr/local/mysql/bin/my_print_defaults
  133. then
  134.   print_defaults="/usr/local/mysql/bin/my_print_defaults"
  135. elif test -x /usr/local/mysql/bin/mysql_print_defaults
  136. then
  137.   print_defaults="/usr/local/mysql/bin/mysql_print_defaults"
  138. else
  139.   print_defaults="my_print_defaults"
  140. fi
  141.  
  142. args=
  143. SET_USER=2
  144. parse_arguments `$print_defaults --loose-verbose $defaults mysqld server`
  145. if test $SET_USER -eq 2
  146. then
  147.   SET_USER=0
  148. fi
  149. parse_arguments `$print_defaults --loose-verbose $defaults mysqld_safe safe_mysqld`
  150. parse_arguments PICK-ARGS-FROM-ARGV "$@"
  151. safe_mysql_unix_port=${mysql_unix_port:-${MYSQL_UNIX_PORT:-/tmp/mysql.sock}}
  152.  
  153. if test ! -x $ledir/$MYSQLD
  154. then
  155.   echo "The file $ledir/$MYSQLD doesn't exist or is not executable"
  156.   echo "Please do a cd to the mysql installation directory and restart"
  157.   echo "this script from there as follows:"
  158.   echo "./bin/mysqld_safe".
  159.   echo "See http://dev.mysql.com/doc/mysql/en/mysqld_safe.html for more"
  160.   echo "information"
  161.   exit 1
  162. fi
  163.  
  164. if test -z "$pid_file"
  165. then
  166.   pid_file=$DATADIR/`/bin/hostname`.pid
  167. else
  168.   case "$pid_file" in
  169.     /* ) ;;
  170.     * )  pid_file="$DATADIR/$pid_file" ;;
  171.   esac
  172. fi
  173. test -z "$err_log"  && err_log=$DATADIR/`/bin/hostname`.err
  174.  
  175. if test -n "$mysql_unix_port"
  176. then
  177.   args="--socket=$mysql_unix_port $args"
  178. fi
  179. if test -n "$mysql_tcp_port"
  180. then
  181.   args="--port=$mysql_tcp_port $args"
  182. fi
  183.  
  184. if test $niceness -eq 0
  185. then
  186.   NOHUP_NICENESS="nohup"
  187. else
  188.   NOHUP_NICENESS="nohup nice -$niceness"
  189. fi
  190.  
  191. # Using nice with no args to get the niceness level is GNU-specific.
  192. # This check could be extended for other operating systems (e.g.,
  193. # BSD could use "nohup sh -c 'ps -o nice -p $$' | tail -1").
  194. # But, it also seems that GNU nohup is the only one which messes
  195. # with the priority, so this is okay.
  196. if nohup nice > /dev/null 2>&1
  197. then
  198.     normal_niceness=`nice`
  199.     nohup_niceness=`nohup nice`
  200.  
  201.     numeric_nice_values=1
  202.     for val in $normal_niceness $nohup_niceness
  203.     do
  204.         case "$val" in
  205.             -[0-9] | -[0-9][0-9] | -[0-9][0-9][0-9] | \
  206.              [0-9] |  [0-9][0-9] |  [0-9][0-9][0-9] )
  207.                 ;;
  208.             * )
  209.                 numeric_nice_values=0 ;;
  210.         esac
  211.     done
  212.  
  213.     if test $numeric_nice_values -eq 1
  214.     then
  215.         nice_value_diff=`expr $nohup_niceness - $normal_niceness`
  216.         if test $? -eq 0 && test $nice_value_diff -gt 0 && \
  217.             nice --$nice_value_diff echo testing > /dev/null 2>&1
  218.         then
  219.             # nohup increases the priority (bad), and we are permitted
  220.             # to lower the priority with respect to the value the user
  221.             # might have been given
  222.             niceness=`expr $niceness - $nice_value_diff`
  223.             NOHUP_NICENESS="nice -$niceness nohup"
  224.         fi
  225.     fi
  226. else
  227.     if nohup echo testing > /dev/null 2>&1
  228.     then
  229.         :
  230.     else
  231.         # nohup doesn't work on this system
  232.         NOHUP_NICENESS=""
  233.     fi
  234. fi
  235.  
  236. USER_OPTION=""
  237. if test -w / -o "$USER" = "root"
  238. then
  239.   if test "$user" != "root" -o $SET_USER = 1
  240.   then
  241.     USER_OPTION="--user=$user"
  242.   fi
  243.   # If we are root, change the err log to the right user.
  244.   touch $err_log; chown $user $err_log
  245.   if test -n "$open_files"
  246.   then
  247.     ulimit -n $open_files
  248.     args="--open-files-limit=$open_files $args"
  249.   fi
  250.   if test -n "$core_file_size"
  251.   then
  252.     ulimit -c $core_file_size
  253.   fi
  254. fi
  255.  
  256. #
  257. # If there exists an old pid file, check if the daemon is already running
  258. # Note: The switches to 'ps' may depend on your operating system
  259. if test -f $pid_file
  260. then
  261.   PID=`cat $pid_file`
  262.   if /bin/kill -0 $PID > /dev/null 2> /dev/null
  263.   then
  264.     if /bin/ps p $PID | grep mysqld > /dev/null
  265.     then    # The pid contains a mysqld process
  266.       echo "A mysqld process already exists"
  267.       echo "A mysqld process already exists at " `date` >> $err_log
  268.       exit 1
  269.     fi
  270.   fi
  271.   rm -f $pid_file
  272.   if test -f $pid_file
  273.   then
  274.     echo "Fatal error: Can't remove the pid file: $pid_file"
  275.     echo "Fatal error: Can't remove the pid file: $pid_file at " `date` >> $err_log
  276.     echo "Please remove it manually and start $0 again"
  277.     echo "mysqld daemon not started"
  278.     exit 1
  279.   fi
  280. fi
  281.  
  282. #
  283. # Uncomment the following lines if you want all tables to be automatically
  284. # checked and repaired during startup. You should add sensible key_buffer
  285. # and sort_buffer values to my.cnf to improve check performance or require
  286. # less disk space.
  287. # Alternatively, you can start mysqld with the "myisam-recover" option. See
  288. # the manual for details.
  289. #
  290. # echo "Checking tables in $DATADIR"
  291. # $MY_BASEDIR_VERSION/bin/myisamchk --silent --force --fast --medium-check $DATADIR/*/*.MYI
  292. # $MY_BASEDIR_VERSION/bin/isamchk --silent --force $DATADIR/*/*.ISM
  293.  
  294. echo "Starting $MYSQLD daemon with databases from $DATADIR"
  295.  
  296. # Does this work on all systems?
  297. #if type ulimit | grep "shell builtin" > /dev/null
  298. #then
  299. #  ulimit -n 256 > /dev/null 2>&1        # Fix for BSD and FreeBSD systems
  300. #fi
  301.  
  302. echo "`date +'%y%m%d %H:%M:%S  mysqld started'`" >> $err_log
  303. while true
  304. do
  305.   rm -f $safe_mysql_unix_port $pid_file    # Some extra safety
  306.   if test -z "$args"
  307.   then
  308.     $NOHUP_NICENESS $ledir/$MYSQLD $defaults --basedir=$MY_BASEDIR_VERSION --datadir=$DATADIR $USER_OPTION --pid-file=$pid_file --skip-locking >> $err_log 2>&1
  309.   else
  310.     eval "$NOHUP_NICENESS $ledir/$MYSQLD $defaults --basedir=$MY_BASEDIR_VERSION --datadir=$DATADIR $USER_OPTION --pid-file=$pid_file --skip-locking $args >> $err_log 2>&1"
  311.   fi
  312.   if test ! -f $pid_file        # This is removed if normal shutdown
  313.   then
  314.     echo "STOPPING server from pid file $pid_file"
  315.     break
  316.   fi
  317.  
  318.   if test true -a $KILL_MYSQLD -eq 1
  319.   then
  320.     # Test if one process was hanging.
  321.     # This is only a fix for Linux (running as base 3 mysqld processes)
  322.     # but should work for the rest of the servers.
  323.     # The only thing is ps x => redhat 5 gives warnings when using ps -x.
  324.     # kill -9 is used or the process won't react on the kill.
  325.     numofproces=`ps xa | grep -v "grep" | grep "$ledir/$MYSQLD\>" | grep -c "pid-file=$pid_file"`
  326.  
  327.     echo -e "\nNumber of processes running now: $numofproces" | tee -a $err_log
  328.     I=1
  329.     while test "$I" -le "$numofproces"
  330.     do 
  331.       PROC=`ps xa | grep "$ledir/$MYSQLD\>" | grep -v "grep" | grep "pid-file=$pid_file" | sed -n '$p'` 
  332.  
  333.       for T in $PROC
  334.       do
  335.         break
  336.       done
  337.       #    echo "TEST $I - $T **"
  338.       if kill -9 $T
  339.       then
  340.         echo "$MYSQLD process hanging, pid $T - killed" | tee -a $err_log
  341.       else 
  342.         break
  343.       fi
  344.       I=`expr $I + 1`
  345.     done
  346.   fi
  347.   echo "`date +'%y%m%d %H:%M:%S'`  mysqld restarted" | tee -a $err_log
  348. done
  349.  
  350. echo "`date +'%y%m%d %H:%M:%S'`  mysqld ended" | tee -a $err_log
  351. echo "" | tee -a $err_log
  352.  
  353.